﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace lchealthy
{
    public partial class frmjf : Form
    {
        DataSet ds;
        int cid=0;
        public frmjf(int id)
        {
            InitializeComponent();
            cid = id;
        }

        private void frmjf_Load(object sender, EventArgs e)
        {
            ddldate.Text = "所有";
            dtps.Value = DateTime.Now;
            dtpe.Value = DateTime.Now;
            bind();

        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            bind();
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddldate.Text == "之间")
            {
                dtps.Visible = true;
                dtpe.Visible = true;
            }
            else if (ddldate.Text == "所有")
            {
                dtps.Visible = false;
                dtpe.Visible = false;
            }
            else
            {
                dtps.Visible = true;
                dtpe.Visible = false;
            }
        }
        private void bind()
        {
            dgvin.AutoGenerateColumns = false;

            string sql;
            sql = "SELECT jifeninfo.[ID], jifenconfig.jifentext, jifenconfig.jnum, jifeninfo.[jdate] FROM jifeninfo LEFT JOIN jifenconfig ON jifeninfo.jid = jifenconfig.ID where cid=" + cid;
            if (ddldate.Text == "大于")
            {
                sql += string.Format(" and jdate>#{0}#", dtps.Value.ToShortDateString());
            }
            if (ddldate.Text == "大于")
            {
                sql += string.Format(" and jdate>#{0}#", dtps.Value.ToShortDateString());
            }
            if (ddldate.Text == "小于")
            {
                sql += string.Format(" and jdate<#{0}#", dtps.Value.ToShortDateString());
            }
            if (ddldate.Text == "等于")
            {
                sql += string.Format(" and jdate =#{0}#", dtps.Value.ToShortDateString());
            }
            if (ddldate.Text == "之间")
            {
                sql += string.Format(" and jdate>#{0}# and jdate<#{1}#", dtps.Value.ToShortDateString(), dtpe.Value.ToShortDateString());
            }
            sql += " order by jdate desc";
            ds = DbHelperOleDb.Query(sql);
            decimal a = 0, s = 0;
            DataRow dr = ds.Tables[0].NewRow();
            dr[1] = "总计：";
            if (ds.Tables[0].Rows.Count > 0)
            {
                s = int.Parse(ds.Tables[0].Compute("sum(jnum)", "").ToString());
                //a = decimal.Parse(ds.Tables[0].Compute("sum(psum)", "").ToString());
            }
            dr[2] = s;
            ds.Tables[0].Rows.Add(dr);
            dgvin.DataSource = ds.Tables[0];
            int widths = 0;
            for (int i = 0; i < dgvin.Columns.Count; i++)
            {
                dgvin.AutoResizeColumn(i, DataGridViewAutoSizeColumnMode.AllCellsExceptHeader);  // 自动调整列宽  
                widths += dgvin.Columns[i].Width;   // 计算调整列后单元列的宽度和                       
            }
            if (widths >= dgvin.Size.Width)  // 如果调整列的宽度大于设定列宽  
                dgvin.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;  // 调整列的模式 自动  
            else
                dgvin.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;  // 如果小于 则填充  
        }
       

        private void btnOut_Click(object sender, EventArgs e)
        {
            myfun.DataToExcel(dgvin);
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            myprint m = new myprint(dgvin);
            m.Print();
        }

        private void btndelete_Click(object sender, EventArgs e)
        {
            int row = dgvin.CurrentRow.Index;
            if (row < dgvin.RowCount - 1)
            {
                int id = int.Parse(ds.Tables[0].Rows[row]["id"].ToString());
                string sql = "delete from jifeninfo where id=" + id;
                DbHelperOleDb.ExecuteSql(sql);
                bind();
            }
        }
    }
}
